import '@js/init.js'
import { studentData, deletStudentData } from '../utils/getData'
let imagesdata = require('../images/default.jpeg');
// let id = 0;
let fanyeNum = $('#fanyeNum').val();
let yeshuNum = 1;
yeshuNum = $('.yeshuNum').text();
let max = 0;
let type = 'name';
let value = '';
fanyeRender(fanyeNum, yeshuNum);
//翻页
async function fanyeRender(fanyeNum, yeshuNum,type, value) {
   
    let data = await studentData({ fanyeNum, yeshuNum, type, value });
    $('.zongshuNum').text(data.fenyeNum);
    $('.zongshuyrNum').text(data.totle);
    max = data.fenyeNum;

    $('.twoNum').text((fanyeNum - 0) + ($('#fanyeNum').val() - 0) * (yeshuNum - 1));
    $('.oneNum').text(($('#fanyeNum').val() - 0) * (yeshuNum - 1) + 1);
    const studentsHtml = data.roes.map(item => {
        return `
                    <tr>
                        <td>${item._id}</td>
                        <td>${item.name}</td>
                        <td>${item.age}</td>
                        <td>${item.sex}</td>
                        <td>${item.objectId ? item.objectId.name : '暂无'}</td>
                        <td>${item.clasId ? item.clasId.name : '暂无'}</td>
                        <td><img src="${item.urlImage|| imagesdata}" alt="" width="50"/></td>
                        <td>
                            <a href="/studentsUpdate.html?id=${item._id}" data-id="${item._id}" id="changeStudent">修改</a>
                            <a href="#" class='delet' data-id="${item._id}">删除</a>
                        </td>
                    </tr>
                `
    }).join('');
   
    $('#studentsTb').html(studentsHtml);
}
//点击下一页
$('#nextye').click(function () {
    yeshuNum++;
    if (yeshuNum > max) {
        yeshuNum = max
    }
    fanyeRender(fanyeNum, yeshuNum);
})
//点击下一页
$('#upye').click(function () {
    yeshuNum--;
    if (yeshuNum < 1) {
        yeshuNum = 1;
    }
    fanyeRender(fanyeNum, yeshuNum);
})
//点击尾页
$('#endye').click(function () {
    yeshuNum = $('.zongshuNum').text();

    fanyeRender(fanyeNum, yeshuNum);
})
//改变数据条数
$('#fanyeNum').change(function () {
    fanyeNum = $(event.target).val();
    $('.twoNum').text(fanyeNum);
    $('.oneNum').text(yeshuNum);
    fanyeRender(fanyeNum, yeshuNum);
})
//点击首页
$('#topye').click(function () {
    yeshuNum = 1;
    fanyeRender(fanyeNum, yeshuNum);
})

//删除
$('#studentsTb').on('click', '.delet', function () {

    event.preventDefault();
    const id = $(event.target).attr('data-id');
    const rem = deletStudentData(id);
    fanyeRender(fanyeNum, yeshuNum);
})

//查询事件
$('#value').change(function () {
    console.log('hhha');
    let type = $('#search').val();
    let value = $('#value').val();
    fanyeRender(fanyeNum, yeshuNum, type, value);
})